<?php
declare (strict_types=1);

namespace app\api\model\v1;

use think\Model;
use think\Route;

/**
 * @mixin \think\Model
 * 项目
 */
class Project extends AppModel
{
    //

    public function project(){
        return $this->belongsTo(Project::class,'project_id');
    }

    //项目指标
    public function target(){
        return $this->hasMany(ProjectTarget::class,'project_id');
    }

    public function sample(){
        return $this->hasMany(ProjectSample::class,'project_id');
    }
    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/29 9:19
     * @param $arrData
     * @param $create_id
     * @return int
     * 处理数据
     */
    public static function saveImportExcel($arrData, $create_id)
    {
        if (!empty($arrData)) {
            foreach ($arrData as $key => $value) {
                $res = self::where(['project_title' => $value['a']])->find();
                if (is_null($res)) {
                    //新增项目
                    $project_id = self::create([
                        'project_title' => $value['a'] ?? '',
                        'uid' => $create_id ?? 0,
                    ])->id;
                    if (isset($value['child'])) { //判断是否包含child字段 具体指标
                        //循环添加入库
                        foreach ($value['child'] as $child_key => $child_value) {
                            ProjectTarget::create([
                                'project_id' => $project_id, //项目id
                                'target_title' => $child_value['b'] ?? '', //具体指标名称
                                'time_limit' => $child_value['c'] ?? 1, //整改时限
                                'score' => $child_value['d'] ?? 0, //分值
                                'standard' => $child_value['e'] ?? '',//备注
                                'duty_unit_id' => 0, //责任单位
                                'uid' => $create_id ?? 0, //导入人
                            ]);
                        }
                    }
                }
            }
            $result = 200;
        } else {
            $result = 500;
        }
        return $result;
    }
}
